home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / sbIDOMEval.js < prev    next >
Text File  |  2006-02-07  |  10KB  |  328 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. //  sbIDOMEval 
  29. //
  30. //  This object scans a DOM tree for attributes containing references to
  31. //  pre-registered variables and later changes the value of all these attributes 
  32. //  according to changes in the values of the variables.
  33. //  ---------------------------------------------------------------------------
  34.  
  35. function sbIDOMEval( domNode )
  36. {
  37.   try
  38.   {
  39.     // Init
  40.     
  41.     this.m_AutoUpdate = false;
  42.     this.m_NeedUpdate = false;
  43.     this.m_NeedScan = true;
  44.     this.m_RootNode = domNode;
  45.     this.m_Nodes = null;     // Array: [0] = element node, [1] [attribute name, attribute value]
  46.     this.m_Variables = null; // Array: [0] = var name, [1] = var last value
  47.  
  48.     //
  49.     // Methods
  50.     //
  51.  
  52.     //  ---------------------------------------------------------------------------
  53.     //  SetValue - Sets the value of a variable, if AutoUpdate is true, the DOM tree is 
  54.     //             updated with the current value of all variables. (see EnableUpdate)
  55.     //  ---------------------------------------------------------------------------
  56.     this.SetValue = function( variable, value )
  57.     {
  58.       try
  59.       {
  60.         var _var = this.GetVariable(variable);
  61.         if (_var == null)
  62.         {
  63.           this.m_NeedScan = true;
  64.           _var = Array(variable, value);
  65.           if (this.m_Variables == null) this.m_Variables = Array();
  66.           this.m_Variables.push(_var);
  67.         }
  68.         else
  69.         {
  70.           _var[1] = value;
  71.         }
  72.         
  73.         // a variable changed, DOM needs updating
  74.         this.m_NeedUpdate = true;
  75.         
  76.         // if executed, this resets m_NeedUpdate to false
  77.         if (this.m_AutoUpdate) this.DoUpdate(); 
  78.       }
  79.       catch ( err )
  80.       {
  81.         alert("SetValue");
  82.         alert( err );
  83.       }
  84.     };
  85.  
  86.     //  ---------------------------------------------------------------------------
  87.     //  GetValue - Get the value of a variable as it was last set
  88.     //  ---------------------------------------------------------------------------
  89.     this.GetValue = function( variable )
  90.     {
  91.       var retval = null;
  92.       try
  93.       {
  94.         var thevar = this.GetVariable(variable);
  95.         if (thevar != null) retval = thevar[1];
  96.       }
  97.       catch ( err )
  98.       {
  99.         alert("GetValue");
  100.         alert( err );
  101.       }
  102.       return retval;
  103.     };
  104.  
  105.     //  ---------------------------------------------------------------------------
  106.     //  GetVariable - Returns a variable entry
  107.     //  ---------------------------------------------------------------------------
  108.     this.GetVariable = function( variable )
  109.     {
  110.       var retval = null;
  111.       try
  112.       {
  113.         if (this.m_Variables != null)
  114.         {
  115.           for (var i=0; i < this.m_Variables.length; i++)
  116.           {
  117.             if (this.m_Variables[i][0] == variable)
  118.             {
  119.               retval = this.m_Variables[i];
  120.               break;
  121.             }
  122.           }
  123.         }
  124.       }
  125.       catch ( err )
  126.       {
  127.         alert("GetVariable");
  128.         alert( err );
  129.       }
  130.       return retval;
  131.     };
  132.  
  133.     //  ---------------------------------------------------------------------------
  134.     //  EnableUpdate - Enable/disable automatic DOM update on SetValue
  135.     //  ---------------------------------------------------------------------------
  136.     this.EnableUpdate = function( tf )
  137.     {
  138.       try
  139.       {
  140.         this.m_AutoUpdate = tf;
  141.         if (this.m_AutoUpdate && this.m_NeedUpdate) 
  142.         {
  143.           this.DoUpdate();
  144.         }
  145.       }
  146.       catch ( err )
  147.       {
  148.         alert("EnableUpdate");
  149.         alert( err );
  150.       }
  151.     };
  152.  
  153.     //  ---------------------------------------------------------------------------
  154.     //  DoUpdate - Update the DOM tree with all the variables values
  155.     //  ---------------------------------------------------------------------------
  156.     this.DoUpdate = function( )
  157.     {
  158.       try
  159.       {
  160.         if (this.m_NeedScan) this.m_NeedUpdate = this.DoScan(this.m_RootNode);
  161.         for (var i = 0; i < this.m_Nodes.length; i++)
  162.         {
  163.           var node =  this.m_Nodes[i][0];
  164.           var attribute = this.m_Nodes[i][1][0];
  165.           var value = this.m_Nodes[i][1][1];
  166.           for (var varnum = 0; varnum < this.m_Variables.length; varnum++)
  167.           {
  168.             var variable = this.m_Variables[i];
  169.             var from = '@'+variable[0]+'@';
  170.             var to = variable[1];
  171.             var p;
  172.             while (1)
  173.             {
  174.               p = this.FindVariable(variable, value);
  175.               if (p < 0) break;
  176.               
  177.               var nv = "";
  178.               if (p > 0) nv += value.substr(0, p); 
  179.               nv += to;
  180.               if (p < value.length-from.length) nv += value.substr(p + from.length);
  181.               value = nv;
  182.             }
  183.           }
  184.           node.setAttribute(attribute, eval(value));
  185.         }
  186.       }
  187.       catch ( err )
  188.       {
  189.         alert("DoUpdate");
  190.         alert( err );
  191.       }
  192.     };
  193.  
  194.     //  ---------------------------------------------------------------------------
  195.     //  DoScan - Scans the DOM for tree with all the variables values
  196.     //  ---------------------------------------------------------------------------
  197.     this.DoScan = function( rootnode )
  198.     {
  199.       if (this.m_RootNode != rootnode) this.m_NeedScan = true;
  200.       this.m_RootNode = rootnode;
  201.       var ret = 0;
  202.       try
  203.       {
  204.         if (this.m_Variables == null)
  205.         {
  206.           ret = 1;
  207.         } 
  208.         else
  209.         {
  210.           // reset node table
  211.           this.m_Nodes = Array();
  212.           
  213.           // scan the tree, remember each reference to a variable
  214.           this.ScanNode(this.m_RootNode);
  215.  
  216.           // no need to rescan
  217.           this.m_NeedScan = false;
  218.         }
  219.       }
  220.       catch ( err )
  221.       {
  222.         alert("DoScan");
  223.         alert( err );
  224.         ret = 1;
  225.       }
  226.       return ret;
  227.     };
  228.  
  229.     //  ---------------------------------------------------------------------------
  230.     //  ScanNode - Scans a node in the DOM (recursive)
  231.     //  ---------------------------------------------------------------------------
  232.     this.ScanNode = function( node )
  233.     {
  234.       try
  235.       {
  236.         this.ScanAttributes(node);
  237.         if (node.childNodes != null)
  238.         {
  239.           for (var i = 0; i < node.childNodes.length; i++)
  240.           {
  241.             this.ScanNode(node.childNodes[i]);
  242.           }
  243.         }
  244.       }
  245.       catch ( err )
  246.       {
  247.         alert("ScanNode");
  248.         alert( err );
  249.       }
  250.     };
  251.  
  252.     //  ---------------------------------------------------------------------------
  253.     //  ScanAttributes - Scans a node for references to any of our variable in any 
  254.     //                   of its attributes
  255.     //  ---------------------------------------------------------------------------
  256.     this.ScanAttributes = function( node )
  257.     {
  258.       try
  259.       {
  260.         if (node.attributes != null)
  261.         {
  262.           for (var i = 0; i < node.attributes.length; i++)
  263.           {
  264.             this.ScanVariables(node, node.attributes[i]);
  265.           }
  266.         }
  267.       }
  268.       catch ( err )
  269.       {
  270.         alert( err );
  271.       }
  272.     };
  273.  
  274.     //  ---------------------------------------------------------------------------
  275.     //  ScanVariables - Scans an attribute for references to any of our variable
  276.     //  ---------------------------------------------------------------------------
  277.     this.ScanVariables = function( node, attribute )
  278.     {
  279.       try
  280.       {
  281.         //attribute.nodeName, attribute.nodeValue
  282.         for (var i = 0; i < this.m_Variables.length; i++)
  283.         {
  284.           if (this.FindVariable(this.m_Variables[i], attribute.nodeValue) >= 0) this.m_Nodes.push(Array(node, Array(attribute.nodeName, attribute.nodeValue)));
  285.         }
  286.       }
  287.       catch ( err )
  288.       {
  289.         alert("ScanVariables");
  290.         alert( err );
  291.       }
  292.     };
  293.  
  294.  
  295.     //  ---------------------------------------------------------------------------
  296.     //  FindVariable - Scans an attribute for references to one variable
  297.     //  ---------------------------------------------------------------------------
  298.     this.FindVariable = function( variable, text )
  299.     {
  300.       var retval = -1;
  301.       try
  302.       {
  303.         retval = text.indexOf("@"+variable[0]+"@");
  304.       }
  305.       catch ( err )
  306.       {
  307.         alert("FindVariable");
  308.         alert( err );
  309.       }
  310.       return retval;
  311.     };
  312.  
  313.     this.Report = function( )
  314.     {
  315.       alert(this.m_Variables + '|' + this.m_Nodes);
  316.     }
  317.  
  318.   }
  319.   catch ( err )
  320.   {
  321.     alert("Global");
  322.     alert( err );
  323.   }
  324.  
  325.   return this;
  326. }
  327.  
  328.